<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('goods_theme_class',LIB_PATH);
$main = new GoodsTheme($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
require_once(INFO_PATH.'cf_brand.php');
switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller, $cf_action, 0);       
        $stype = isset($_POST['stype']) ? $_POST['stype'] : $_GET['stype'];        
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));       
        $where = "1";

        if ($skey) {
            $where .= " AND news_title like '%" . $skey . "%'";
        }
		if ($stype) {
            $where .= " AND news_sort ='" . $stype . "'";
        }
        load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		$field = '*';		
        $data = $MyPage->from_sql($field, $main->get_table_name(), $where, "gt_id DESC", intval($_GET['cpage']), "20");		   
		$page = $MyPage->get_page($data, $cf_self_page . '?dpt='.$cf_dpt.'&skey=' . $skey . '&stype=' . $stype);
		
	break;
	case "add":
		if($cf_ispost){
			$data = array(
						'gt_title'=>trim($_POST['gt_title']),
						'gt_description'=>$_POST['gt_description'],
						'gt_coupon'=>$_POST['gt_coupon'],
						'gt_image'=>$_POST['gt_image'],
						'gt_price'=>$_POST['gt_price'],
						'gt_is_show'=>$_POST['gt_is_show'],
						'gt_goods_id'=>implode(",",$_POST['goods_id']),
						'gt_create_time'=>date("Y-m-d H:i:s")
					);	
			//$db->debug(1,1);				
			$result = $main->insert_data($data);
			if($result){
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
				$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
				my_jump('添加成功'.$more,'',$return_page.'?dpt='.$cf_dpt,3);
			}else{
			my_jump('添加失败');
			}
		}else{	
			$smarty->mypage($cf_controller,$cf_action,0);	
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';	
		}

    break;
	case "edit":
		if($cf_ispost){
			$data = array(
						'gt_title'=>trim($_POST['gt_title']),
						'gt_description'=>$_POST['gt_description'],
						'gt_coupon'=>$_POST['gt_coupon'],
						'gt_image'=>$_POST['gt_image'],
						'gt_price'=>$_POST['gt_price'],
						'gt_is_show'=>$_POST['gt_is_show'],
						'gt_goods_id'=>implode(",",$_POST['goods_id']),
						'gt_edit_time'=>date("Y-m-d H:i:s")
					);			
			$where = "gt_id = '".$id."'";
			$result = $main->update_data($data,$where);
			if($result){
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
				
				my_jump('修改成功'.$more,'',$return_page.'?dpt='.$cf_dpt,3);
			}else{
				my_jump('修改失败');
			}
		}else{		
			$smarty->mypage($cf_controller,$cf_action,0);	
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
			$row = $main->get_data_row($id);
			if($row['gt_goods_id']){				//查询商品列表				
				$row['goods_list'] = $db->getAll("SELECT goods_id,goods_name FROM data_goods WHERE goods_id IN (".$row['gt_goods_id'].")");
			}			
		}

    break;
	case "goods":
		$smarty->mypage('goods_list',$cf_action,0);
		load_file('goods_class',LIB_PATH);
		$goods = new Goods($db);
		$data = $goods->get_data_array("goods_brand_id = '".$id."' AND goods_status = '1'",'goods_id DESC','','goods_id,goods_name');
		//dump($data);
		
	break;	
	
	default:
		my_jump('非法操作!');
	break;
}

$smarty->assign(array(
	'id'=>$id,
	'rid'=>$rid,
	'row'=>$row,
	'data'=>$data,	
	'page'=>$page,
	'skey' => $skey,
	'stype' => $stype,
	'cf_brand' => add_array_item(array(''=>'请选择品牌'),$cf_brand),
	'cf_action' => $cf_action,

));
$smarty->mydisplay();

?>